package com.inspur.gs.sbssdl.sbyxgl.djjh.djjh.front.djjhxz_frm.voactions;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.parameter.retrieve.RetrieveParam;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bff.spi.*;
import com.inspur.edp.bef.api.action.*;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class xzdatasVOAction extends AbstractFSAction<VoidActionResult> {

	private String id;
	private Object[] data;
	public xzdatasVOAction(String id, Object[] data) {
		this.id = id;
		this.data = data;
	}
	@Override
	public void execute() {
		System.out.println(id);
		List<djpzRecord> djpzlist = new ArrayList<>();
		for (Object datum : data) {
			djpzRecord record = parseRecord(datum.toString());
			djpzlist.add(record);
			System.out.println(datum.toString());
		}
		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
//			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
//			String djxzid = "ff554cce-1197-469a-88f1-814f2ed0781d";// 设备点检配置明细id
//			IStandardLcp djxzlcp = iLcpFactory.createLcpByBEId(djxzid);
//
//			// 主表变更
//			ModifyChangeDetail curChange = new ModifyChangeDetail(id);
//
//			//新增一条子表数据
//			for (djpzRecord item : djpzlist) {
//				//新增一条子表数据
//				ArrayList<String> nodeCodes = new ArrayList();
//				nodeCodes.add("sbdjpzmx");
//				ArrayList<String> hierarchyIds = new ArrayList();
//				hierarchyIds.add(id);
//				IEntityData djpzmx = djxzlcp.retrieveDefaultChild(nodeCodes, hierarchyIds);
//				//子表关联
//				ModifyChangeDetail childChange = new ModifyChangeDetail(djpzmx.getID());
//				childChange.getPropertyChanges().put("djjg",item.getDjjg());
//				childChange.getPropertyChanges().put("djbw",item.getDjbw());
//				childChange.getPropertyChanges().put("djnr",item.getDjnr());
//				childChange.getPropertyChanges().put("bwnr",item.getBwsm());
//				childChange.getPropertyChanges().put("zysx",item.getZysx());
//				//变更值
//				curChange.addChildChangeSet("sbdjpzmx", childChange);
//			}
//			djxzlcp.modify(curChange);
//			djxzlcp.save();

			//点检配置变更
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			String djid = "ff554cce-1197-469a-88f1-814f2ed0781d";// 设备点检配置id
			IStandardLcp djlcp = iLcpFactory.createLcpByBEId(djid);
			// 主表变更
			ModifyChangeDetail change = new ModifyChangeDetail(id);
			change.getPropertyChanges().put("djpz",true);
			djlcp.modify(change);
			djlcp.save();

			//明细变更
			String djxzid = "d63f2f8e-2c5d-441d-9ba7-1edc3712159c";// 设备点检配置明细id
			IStandardLcp djxzlcp = iLcpFactory.createLcpByBEId(djxzid);

			for (djpzRecord item : djpzlist) {
				IEntityData newData=djxzlcp.retrieveDefault();
				ModifyChangeDetail curChange = new ModifyChangeDetail(newData.getID());
				curChange.getPropertyChanges().put("ParentID",id);
				curChange.getPropertyChanges().put("djjg",item.getDjjg());
				curChange.getPropertyChanges().put("djbw",item.getDjbw());
				curChange.getPropertyChanges().put("djnr",item.getDjnr());
				curChange.getPropertyChanges().put("bwnr",item.getBwsm());
				curChange.getPropertyChanges().put("zysx",item.getZysx());
				djxzlcp.modify(curChange);
			}
			djxzlcp.save();

		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}

	}


	public static class djpzRecord {
		private String sbdl;
		private String sbxl;
		private String djjg;
		private String djbw;
		private String djnr;
		private String bwsm;
		private String zysx;

		public String getSbdl() {
			return sbdl;
		}

		public void setSbdl(String sbdl) {
			this.sbdl = sbdl;
		}

		public String getSbxl() {
			return sbxl;
		}

		public void setSbxl(String sbxl) {
			this.sbxl = sbxl;
		}

		public String getDjjg() {
			return djjg;
		}

		public void setDjjg(String djjg) {
			this.djjg = djjg;
		}

		public String getDjbw() {
			return djbw;
		}

		public void setDjbw(String djbw) {
			this.djbw = djbw;
		}

		public String getDjnr() {
			return djnr;
		}

		public void setDjnr(String djnr) {
			this.djnr = djnr;
		}

		public String getBwsm() {
			return bwsm;
		}

		public void setBwsm(String bwsm) {
			this.bwsm = bwsm;
		}

		public String getZysx() {
			return zysx;
		}

		public void setZysx(String zysx) {
			this.zysx = zysx;
		}

	}

	public static djpzRecord parseRecord(String recordStr) {
		String content = recordStr.substring(1, recordStr.length() - 1);
		String[] pairs = content.split(", ");

		Map<String, String> map = new HashMap<>();
		for (String pair : pairs) {
			String[] kv = pair.split("=", 2);
			if (kv.length == 2) {
				map.put(kv[0], kv[1].equals("null") ? null : kv[1]);
			}
		}

		djpzRecord record = new djpzRecord();
		try {
			record.setSbdl(map.get("sbdl"));
			record.setSbxl(map.get("sbxl"));
			record.setDjjg(map.get("djjg"));
			record.setDjbw(map.get("djbw"));
			record.setDjnr(map.get("djnr"));
			record.setBwsm(map.get("bwsm"));
			record.setZysx(map.get("zysx"));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return record;
	}

}
